package com.suoluo.zenadmin.controllers.portal.classify;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.suoluo.zenadmin.common.PageUtil;
import com.suoluo.zenadmin.common.UBB;
import com.suoluo.zenadmin.common.config.SystemConfigHolder;
import com.suoluo.zenadmin.service.TaskExecutor;
import com.suoluo.zenadmin.service.bigimage.BigImageService;
import com.suoluo.zenadmin.service.classify.StyleService;
import com.suoluo.zenadmin.service.dest.DestFeatureCategoryService;
import com.suoluo.zenadmin.service.dest.DestService;
import com.suoluo.zenadmin.service.news.NewsService;
import com.suoluo.zenadmin.service.trip.TripService;
import com.suoluo.zenadmin.vo.BaseInfoVO;
import com.suoluo.zenadmin.vo.bigimage.BigImageVO;
import com.suoluo.zenadmin.vo.dest.DestFeatureCategoryVO;
import com.suoluo.zenadmin.vo.dest.DestFeatureItem2VO;
import com.suoluo.zenadmin.vo.news.News2VO;
import com.suoluo.zenadmin.vo.trip.TripVO;

@Path("")
public class StyleController {

//	/**
//	 * 新闻
//	 */
//	private static final int NEWS = -1;
//	/**
//	 * 目的地
//	 */
//	private static final int DEST = -2;
//	/**
//	 * 酒店
//	 */
//	private static final int HOTEL = -3;
//	/**
//	 * 最新
//	 */
//	private static final int LATEST = -4;
//	/**
//	 * 行程
//	 */
//	private static final int TRIP = -5;
	
	private static final Log logger = LogFactory.getLog(StyleController.class);
	@Resource(name="styleService")
	private StyleService styleService;
	@Resource(name="destService")
	private DestService destService;
	
	@Resource(name="categoryService")
	private DestFeatureCategoryService categoryService;


	@Resource(name="newsService")
	private NewsService newsService;
	@Resource(name="tripService")
	private TripService tripService;
//	@Resource(name="countryService")
//	private CountryService countryService;
	
	@Resource(name="taskExecutor")
	private TaskExecutor taskExecutor;
	
	@Resource(name="bigImageService")
	private BigImageService bigImageService;
	
	private static final int page_rows = 12;
	
	private void showClassifyHead(Invocation inv,int featureCategoryId){
		List<BigImageVO> bigImages = bigImageService.findBigImageByType(BigImageVO.IMAGE_TYPE_NEWS);
		inv.addModel("bigImages",bigImages);
//		String from = inv.getRequest().getHeader("Referer");
		//需要过滤掉特别体验
		List<DestFeatureCategoryVO> categories = categoryService.findRootCategoryWithoutHotel();
		
		inv.addModel("categories",categories);
		
		//特别体验的编号
		int specialId = SystemConfigHolder.getDestFeatureCategorySpecial();
		inv.addModel("specialId", specialId);
		
		//过滤掉特别体验
		Iterator<DestFeatureCategoryVO> ite = categories.iterator();
		while (ite.hasNext()){
			DestFeatureCategoryVO c = ite.next();
			if (c.getFeatureCategoryId() == featureCategoryId){
				inv.addModel("curCategory",c);
			}
			
			if (c.getFeatureCategoryId() == specialId){
				ite.remove();
			}
			
		}
		
	}
	
	/**
	 * 风格偏好下查询“最新”菜单的信息
	 * @param request
	 * @param response
	 * @return
	 */
	@Get("")
	public String showClassify(Invocation inv)
	{
		//显示头部信息
		showClassifyHead(inv,0);
		
		//查询第一页的新闻数据
		List<News2VO> newses = newsService.findNewsWithoutContent(1,page_rows);
		inv.addModel("newses", newses);
		//查询总记录数
		int totalCount =  newsService.findNewsCount();
		inv.addModel("totalCount",totalCount);
		
		//计算总页数
		int maxPage = PageUtil.calcPageCount(totalCount, page_rows);
		inv.addModel("maxPage",maxPage);
		return "listClassifyNews";
	}
	
	@Get("news/page/{page:[0-9]+}")
	public String showNewsPage(Invocation inv,@Param("page")int page){
		//查询第一页的新闻数据
		List<News2VO> newses = newsService.findNewsWithoutContent(page,page_rows);
		inv.addModel("newses", newses);
		return "newspage";
	}
	
	/**
	 * 查询新闻详细信息
	 * @param request
	 * @param response
	 * @return
	 */
	@Get("news/{newsId:[0-9]+}")
	public String showNews(Invocation invo,@Param("newsId")int newsId)
	{
		//增加新闻的浏览量
		taskExecutor.submitViewNews(newsId);
		
//		showClassifyHead(invo, 0);
		//查询新闻信息
		News2VO news = newsService.findNewsById(newsId);
		List<BaseInfoVO> subNewses = newsService.findSubNews(newsId);
		for (BaseInfoVO subNews : subNewses){
			subNews.setContent(UBB.decode(subNews.getContent()));
		}
		news.setSubNews(subNewses);
		invo.addModel("news",news);
		
		//查询新闻三条类似的新闻
		int pageSize = SystemConfigHolder.getStyleNewsPageSize();
		List<News2VO> newsList =newsService.findSimilarNews(newsId,news.getFeatureCategoryId(),pageSize);
		invo.addModel("newsList",newsList);
		return "news";
	}
	
	/**
	 * 查询特色项列表
	 * @param request
	 * @param response
	 * @return
	 */
	@Get("item/{fcid:[0-9]+}")
	public String showFeatureItems(Invocation inv,@Param("fcid")int featureCategoryId)
	{
		//显示头部信息
		showClassifyHead(inv,featureCategoryId);
		//查询第一页的特色项数据
		List<DestFeatureItem2VO> items = styleService.findFeatureItemByCategoryId(featureCategoryId, 1,page_rows);
		inv.addModel("items", items);
		//查询总记录数
		int totalCount = styleService.findFeatureItemCountByCategoryId(featureCategoryId);
		inv.addModel("totalCount",totalCount);
		
		//计算总页数
		int maxPage = PageUtil.calcPageCount(totalCount, page_rows);
		inv.addModel("maxPage",maxPage);
		return "listClassifyFeatureItems";
	}
	
	@Get("item/{fcid:[0-9]+}/page/{page:[0-9]+}")
	public String showItemPage(Invocation inv, @Param("fcid")int featureCategoryId,@Param("page") int page){
//		${cpath }/classify/item/${curCategory.featureCategoryId}/page/
		DestFeatureCategoryVO  curCategory = categoryService.findDestFeatureCategoryById(featureCategoryId);
		inv.addModel("curCategory",curCategory);
		//查询第一页的特色项数据
		List<DestFeatureItem2VO> items = styleService.findFeatureItemByCategoryId(featureCategoryId, page,page_rows);
		inv.addModel("items", items);
		return "itempage";
	}
	
	/**
	 * 特色项详细信息
	 * @param request
	 * @param response
	 * @return
	 */
	@Get("item/{fcid:[0-9]+}/{id:[0-9]+}")
	public String showFeatureItem(Invocation inv,@Param("fcid")int categoryId,@Param("id")int featureItemId)
	{
		//显示头部信息
//		showClassifyHead(inv,categoryId);
		
		DestFeatureCategoryVO  curCategory = categoryService.findDestFeatureCategoryById(categoryId);
		inv.addModel("curCategory",curCategory);
		
		DestFeatureItem2VO featureItem = destService.findCompleteFeatureItemById(featureItemId);
		inv.addModel("featureItem", featureItem);
		
		/*
		 * 特别体验单独一种风格展示 
		 */
		if (categoryId == SystemConfigHolder.getDestFeatureCategorySpecial()){
			List<DestFeatureItem2VO> samilarFeatureItems = destService.findSamilarDestFeatureItems(featureItemId, featureItem.getFeatureId(), 3);
			inv.addModel("samilarFeatureItems", samilarFeatureItems);
			return "experience";
		}else {
			List<DestFeatureItem2VO> samilarFeatureItems = destService.findSamilarDestFeatureItems(featureItemId, featureItem.getFeatureId(), 4);
			inv.addModel("samilarFeatureItems", samilarFeatureItems);
			return "featureItem";
		}
	}
	
	/**
	 * 获取行程线路的数据列表
	 * @param request
	 * @param response
	 * @return
	 */
	@Get("trip")
	public String trip(Invocation inv)
	{
		//显示头部信息
		showClassifyHead(inv,0);
		//查询第一页的特色项数据
		List<TripVO> trips = tripService.findAuditedPageTrips(1, page_rows);
		inv.addModel("trips", trips);
		int totalCount = tripService.findAuditedTripCount();
		//查询总记录数
		inv.addModel("totalCount",totalCount);
		
		//计算总页数
		int maxPage = PageUtil.calcPageCount(totalCount, page_rows);
		inv.addModel("maxPage",maxPage);
		return "listClassifyTrip";
	}
	
	@Get("news/page/{page:[0-9]+}")
	public String showTripPage(Invocation inv,@Param("page")int page){
		//查询当前页的行程
		List<TripVO> trips = tripService.findListTrip(page,page_rows);
		inv.addModel("trips", trips);
		return "trippage";
	}
	
	/**
	 * 风格偏好下查询新闻列表
	 * @param request
	 * @param response
	 * @return
	 */
	@Get("news")
	public String showNewses(Invocation inv)
	{
		return "r:"+ inv.getRequestPath().getCtxpath()+"/classify";
	}
	
	/**
	 * 风格偏好下查询目的地列表
	 * @param request
	 * @param response
	 * @return
	 */
	@Get("destination")
	public String showDests(Invocation inv)
	{
		return "r:"+ inv.getRequestPath().getCtxpath()+"/destination";
	}
	
	/**
	 * 风格偏好下查询住宿（酒店）列表
	 * @param request
	 * @param response
	 * @return
	 */
	@Get("hotel")
	public String showHotels(Invocation inv)
	{
		return "r:"+ inv.getRequestPath().getCtxpath()+"/hotel";
	}
}
